<template>
  <div class="top-category">
    <div class="container">
      <!-- 面包屑 -->
      <el-breadcrumb separator="/" class="breadcrumb">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <transition name="fade-right" mode="out-in">
             <el-breadcrumb-item :key="topCategory.id">{{topCategory.name}}</el-breadcrumb-item>
        </transition>
      </el-breadcrumb>
      <!-- 轮播图 -->
      <Slideshow :slides="sliders" style="height:500px" :autoPlay="true"></Slideshow>
      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="sub in topCategory.children" :key="sub">
            <Router-link :to="`sub/${sub.id}`">
              <img :src="sub.picture" >
              <p>{{sub.name}}</p>
            </Router-link>
          </li>
        </ul>
      </div>
    </div>
    <!-- 所有二级分类 分类关联商品-->
      <div class="ref-goods" v-for="sub in subList" :key="sub.id">
        <div class="head">
          <h3>&lt;&lt;  {{sub.name}} &gt;&gt;</h3>
          <p class="tag">温暖柔软，品质之选</p>
          <More :path="`/category/sub/${sub.id}`" />
        </div>
        <div class="body">
          <GoodsItem v-for="goods in sub.goods" :key="goods.id" :goods="goods"/>
        </div>
      </div>
  </div>
</template>

<script>
import { computed, ref, watch } from 'vue'
import { findBanner } from '../../api/home'
import { findTopCategory } from '../../api/category'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import GoodsItem from './components/goods-item.vue'
// import More from '../../components/librarys/more.vue';
export default {
  name: 'Category',
  setup () {
    // 轮播图
    const sliders = ref([])
    findBanner().then(data => {
      sliders.value = data.result
    })
    // 面包屑加所有子类   vuex
    const store = useStore()
    const route = useRoute()
    const topCategory = computed(() => {
      // 当前分类 == 根据路由上id去vuex中category模块的list查找
      let cate = {}
      // console.log(store.state.category.list)
      // console.log(route.params)
      const categoryItem = store.state.category.list.find(item => {
        return item.id === route.params.id
      })
      if (categoryItem) {
        cate = categoryItem
      }
      return cate
    })
    // 获取各个子类目录下推荐商品
    const subList = ref([])
    // 不监听的话只发送一次请求
    const getSubList = () => {
      findTopCategory(route.params.id).then(data => {
        subList.value = data.result.children
      })
    }
    watch(() => route.params.id, (newVal) => {
      // 在顶级类目下才发请求
      if (newVal && `/category/${newVal}` === route.path) {
        getSubList()
      }
      // console.log(route)
      // newVal && getSubList()
    }, { immediate: true })
    return { sliders, topCategory, subList }
  },
  components: {
    GoodsItem
  }
}
</script>
<style lang="less" scoped>
@import '../../assets/styles/variable.less';
// 面包屑动画
.fade-right-enter-from{
  transform: translateX(20px);
  opacity: 0;
}
.fade-right-enter-active{
  transition: all 1s ease;
}
.fade-right-enter-to{
  transform: translateX(0);
  opacity: 1;
}
.fade-right-leave-from{
  transform: translateX(0);
  opacity: 1;
}
.fade-right-leave-to{
  transform: translateX(-20px);
  opacity: 0;
}
.breadcrumb{
  padding-bottom: 20px;
}
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @c;
          }
        }
      }
    }
  }
    .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;
      padding: 0 65px 30px;
    }
  }
}
</style>
